package thunderbirdgtd.entities;

import java.util.LinkedList;
import java.util.ListIterator;

public class Project extends BaseActionOrProject {
	public  LinkedList<Project> assignedProjects = new LinkedList<Project>();
	private Project parentProject = null;
	private static int id_proj = 0;
	public LinkedList<Action> assignedActions= new LinkedList<Action>();

	public Project(String name) {
		this.name = name;
		this.id = id_proj++;
		assignedProjects = new LinkedList<Project>();
		assignedTags = new LinkedList<Tag>();
	}
	
	public Project(String name, int id) {
		this.name = name;
		this.id = id;
		if(id > id_proj)
			id_proj = id+1;
		assignedProjects = new LinkedList<Project>();
		assignedTags = new LinkedList<Tag>();
	}

	public static LinkedList<Project> findProjectByTag(
			LinkedList<Project> projects, Tag tag) {
		LinkedList<Project> tagsProjects = new LinkedList<Project>();

		java.util.ListIterator<Project> li = projects.listIterator();
		for (Project p = projects.getFirst(); li.hasNext(); p = (Project) li
				.next()) {
			if (p.hasTag(tag))
				tagsProjects.add(p);
		}

		return tagsProjects;
	}

	public boolean assignSubProject(Project p) {
		try {
			assignedProjects.add(p);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public LinkedList<Project> getSubProjects() {
		return assignedProjects;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public String toString() {
		String parent = "None";
		String priority = "None";
		if(getParentProject()!=null)
			parent = String.valueOf(getParentProject().getId());
		if(getPriority()!=null)
			priority = getPriority().toString();
		return "Name: " + name + " ID: " + id + " Parent: " + parent + " Priority: " + priority;
	}

	public Project getParentProject() {
		return parentProject;
	}

	public void setParentProject(Project parentProject) {
		this.parentProject = parentProject;
	}

	public static Project searchProjectInLinkedList(String name,	LinkedList<Project> projects) throws Exception {

		if (projects == null)
			throw new Exception("null list");
		ListIterator<Project> li = (ListIterator<Project>) projects.listIterator();
		Project p = null;
		while(li.hasNext()){
			p = li.next();
			if (p.name.equals(name))
				return p;
		}
		return null;
	}
	
	public void addAction(Action actionToAdd) throws Exception{
		
		if (actionToAdd == null)
			throw new Exception("Null action to add.");
		
		if (assignedActions == null)
			assignedActions = new LinkedList<Action>();
	
		assignedActions.add(actionToAdd);
		actionToAdd.setParentProject(this);
	}
	
	public void addAlarmProject(Alarm al) throws Exception{
		if(al==null)
			throw new Exception("Null alarm to add.");
		LinkedList<Alarm> listAlarms = this.getAlarms();
		
		if (listAlarms == null)
			listAlarms = new LinkedList<Alarm>();
		if (listAlarms.contains(al)==false)	{
			listAlarms.add(al);
		}
		else
			throw new Exception("Alarm already applied.");
			
	}
	
	public LinkedList<Action> getActions() {
		return assignedActions;
	}
	
	

	public void removeAction(Action a) {
		assignedActions.remove(a);		
	}

	public boolean hasSubProjects() {
		return assignedProjects.size() != 0;
	}

	public boolean isSubProject() {
		return parentProject != null;
	}
}
